#include "DXUT.h"
#include "RenderStaticMesh.h"
#include "..\\IO\\Collada\\ColladaRender.h"

CRenderStaticMesh::CRenderStaticMesh(void):RenderTech()
{
	m_pModelRender = NULL;
	m_pEffect = NULL;
	m_pRenderTech = NULL;
}


CRenderStaticMesh::~CRenderStaticMesh(void)
{
	OnD3D11DestroyDevice(NULL);
	SAFE_DELETE(m_pModelRender);
}

HRESULT CRenderStaticMesh::OnD3D11CreateDevice(ID3D11Device* pd3dDevice, 
	const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, CDirectoryUtil* pDirUtil, void* pUserContext)
{
	HRESULT hr = S_OK;

    if(!m_pModelRender||!m_pModelRender->isModelReady())
	{
			printf("Please load the model firstly!!!\n");
			return E_FAIL;
	}
	//create effects
	TCHAR ShaderFileDir[MAX_PATH];
	enum SUBDIRECTORY subfolder = SUBDIRECTORY::SHADERSDIR;
	pDirUtil->GetAbsoluteFileFolder(L"RenderStaticMesh.fx",subfolder, ShaderFileDir);
	V_RETURN(CD3DUtilityFunction::CreateEffectsFromFile(pd3dDevice, ShaderFileDir, NULL, "fx_5_0", &m_pEffect));	
	InitEffectsVariables();
	//call createdevice function for mesh
    V_RETURN(m_pModelRender->OnD3D11CreateDevice(pd3dDevice, m_pRenderTech, pBackBufferSurfaceDesc, pDirUtil, pUserContext));
	
	return hr;
}

void CRenderStaticMesh::InitEffectsVariables()
{
	m_pRenderTech = m_pEffect->GetTechniqueByName(RENDERSTATICMESHTECHS[m_pModelRender->GetVertType()]);
}

void CRenderStaticMesh::ReloadEffectsDebug(ID3D11Device* pd3dDevice, CDirectoryUtil* pDirUtil)
{
	HRESULT hr = S_OK;
	SAFE_RELEASE(m_pEffect);
	//create effects
	TCHAR ShaderFileDir[MAX_PATH];
	enum SUBDIRECTORY subfolder = SUBDIRECTORY::SHADERSDIR;
	pDirUtil->GetAbsoluteFileFolder(L"RenderStaticMesh.fx",subfolder, ShaderFileDir);
	hr = CD3DUtilityFunction::CreateEffectsFromFile(pd3dDevice, ShaderFileDir, NULL, "fx_5_0", &m_pEffect);	
	InitEffectsVariables();
}

HRESULT CRenderStaticMesh::OnD3D11ResizedSwapChain( ID3D11Device* pd3dDevice, 
	IDXGISwapChain* pSwapChain, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, CDirectoryUtil* pDirUtil, void* pUserContext )
{
	if(m_pModelRender)
		return m_pModelRender->OnD3D11ResizedSwapChain(pd3dDevice,pSwapChain,pBackBufferSurfaceDesc,pDirUtil,pUserContext);	
	else
		return S_OK;
}

void CRenderStaticMesh::OnD3D11ReleasingSwapChain( void* pUserContext )
{
	if(m_pModelRender)
		m_pModelRender->OnD3D11ReleasingSwapChain(pUserContext);
}

void CRenderStaticMesh::OnD3D11DestroyDevice( void* pUserContext )
{
	if(m_pModelRender)
		m_pModelRender->OnD3D11DestroyDevice(pUserContext);
	SAFE_RELEASE(m_pEffect);
}

void CRenderStaticMesh::OnD3D11FrameRender( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, 
	CBaseCamera* pViewCamera, CDirectoryUtil* pDirUtil, double fTime, float fElapsedTime, void* pUserContext )
{
	if(!m_pModelRender||!m_pModelRender->isModelReady())
	{
		printf("Please load the model firstly!!!\n");
		return;
	}
	HRESULT hr = S_OK;
	// Get the projection & view matrix from the camera class
	CModelViewerCamera* pCurCamera = static_cast<CModelViewerCamera*>(pViewCamera);
	D3DXMATRIX mWorld = *(pCurCamera->GetWorldMatrix());
	D3DXMATRIX mView = *(pCurCamera->GetViewMatrix());
	D3DXMATRIX mProj = *(pCurCamera->GetProjMatrix());
	D3DXMATRIX mViewProjection = mView * mProj;
	hr = (m_pEffect->GetVariableByName("mViewProjection")->AsMatrix())->SetMatrix((float*)(&mViewProjection));

	m_pModelRender->Render(pd3dDevice,pd3dImmediateContext,m_pEffect,m_pRenderTech,RENDERMESHTECH::RENDERSTATICMESH,mWorld,
		                   fTime,fElapsedTime,pUserContext);
}